﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SalaryChannel5._0
{
    public partial class FilterCombox : UserControl
    {
        private List<string> _items;
        public List<string> Items
        {
            get
            {
                return _items;
            }
            set
            {
                if (_items != value)
                {
                    _items = value;
                    this.FilterCBox.Items.Clear();
                    foreach (var i in _items)
                    {
                        this.FilterCBox.Items.Add(i);
                    }
                    this.FilterCBox.SelectedIndex = 0;
                    this.FilterTB.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
          
        }
        public string SeletedText
        {
            get
            {
                return this.FilterTB.Text;
            }
            set
            {
                if (this.FilterTB.Text != value)
                {
                    this.FilterTB.Text = value;
                    this.FilterCBox.SelectedItem = value;
                }
            }
        }

        public int SelectedIndex
        {
            get
            {
                return this.FilterCBox.SelectedIndex;
            }
            set
            {
                if (this.FilterCBox.SelectedIndex != value)
                {
                    this.FilterCBox.SelectedIndex = value;
                }
            }
        }
        public new double Height
        {
            get
            {
                return this.Height;
            }
            set
            {
                if (this.Height != value)
                {
                    this.Height = value;
                    this.FilterCBox.Height = value;
                    this.FilterTB.Height = value;
                }

            }
        }

        public new double Width
        {
            get
            {
                return this.Width;
            }

            set
            {
                if (this.Width != value)
                {
                    this.Width = value;
                    this.FilterCBox.Width = value;
                    this.FilterTB.Width = value;
                }

            }
        }

        public FilterCombox()
        {
            InitializeComponent();

            _items = new List<string>();
           // this.FilterTB.KeyDown+=new KeyEventHandler(FilterTB_KeyDown);
            this.FilterTB.MouseEnter+=new MouseEventHandler(FilterTB_MouseEnter);
            this.FilterTB.LostFocus += new RoutedEventHandler(FilterTB_LostFocus);
            FilterTB.TextChanged += new TextChangedEventHandler(FilterTB_TextChanged);
            FilterTB.KeyDown += new KeyEventHandler(FilterTB_KeyDown);

            this.FilterCBox.SelectionChanged += new SelectionChangedEventHandler(FilterCBox_SelectionChanged);
            this.FilterCBox.DropDownOpened += new EventHandler(FilterCBox_DropDownOpened);

            FilterTB.Visibility = System.Windows.Visibility.Collapsed;
        
        }

        void FilterTB_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                if (FilterCBox.Items.Count > 0)
                {
                    FilterCBox.SelectedIndex = 0;
                    FilterTB.Visibility = System.Windows.Visibility.Collapsed;
                    FilterCBox.IsDropDownOpen = false;
                }
            }
        }

       
        
        //bool IsAutoOpen = true;
        void FilterCBox_DropDownOpened(object sender, EventArgs e)
        {
            //if (!IsAutoOpen) {  return; }
            if (this.FilterTB.Visibility == System.Windows.Visibility.Collapsed)
            {
                this.FilterTB.Visibility = System.Windows.Visibility.Visible;
                FilterTB.SelectAll();
                FilterTB.Focus();
                //this.FilterCBox.IsDropDownOpen = true;
                //IsAutoOpen = false;
            }
        }

       

    
        public void Clear()
        {
            _items.Clear();
            this.FilterCBox.Items.Clear();
            this.FilterTB.Text = string.Empty;
            
        }
        void FilterTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            string keyStr = FilterTB.Text.Trim().ToLower();
            if (FilterTB.Tag == null
                || !FilterTB.Tag.Equals(keyStr))
            {
                this.FilterCBox.Items.Clear();
                if (!string.IsNullOrEmpty(keyStr))
                {
                    var query = from q in this._items
                                where q.ToString().ToLower().Contains(keyStr)
                                select q;

                    foreach (var i in query)
                    {
                        this.FilterCBox.Items.Add(i);
                    }
                }
                else
                {
                    foreach (var i in _items)
                    {
                        this.FilterCBox.Items.Add(i);
                    }
                }
                FilterCBox.IsDropDownOpen = true;
                FilterTB.Tag = keyStr;
                
                //FilterTB.Visibility = System.Windows.Visibility.Visible;
            }
        }

        void FilterCBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.FilterTB.Visibility = System.Windows.Visibility.Collapsed;
            if (FilterCBox.SelectedItem != null)
            {
                this.FilterTB.Text = FilterCBox.SelectedItem.ToString();
                this.FilterTB.Tag = FilterTB.Text;
            }
            else
            {
                if (FilterTB.Visibility == System.Windows.Visibility.Collapsed)
                {
                    FilterTB.Text = string.Empty;
                }
            }
        }
        /// <summary>
        /// 显示文本输入框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FilterTB_MouseEnter(object sender, MouseEventArgs e)
        {
            this.FilterTB.Visibility = System.Windows.Visibility.Visible;
        }

        /// <summary>
        /// 显示Combox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void FilterTB_LostFocus(object sender, RoutedEventArgs e)
        {
            if (FilterCBox.IsDropDownOpen == false)
            {
                this.FilterTB.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
    }
}
